package com.xy.smartsms.db.base;

import android.content.ContentResolver;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;

import com.xy.smartsms.db.carrierparam.CarrierDBManager;
import com.xy.smartsms.util.Log;

import cn.com.xy.sms.sdk.constant.Constant;

public class MixCursor {
    SQLiteDatabase mySQLiteDatabase = null;
    Cursor cur = null;
    int type = 0;

    public MixCursor(SQLiteDatabase mySQLiteDatabase, Cursor cur, int type) {
        this.mySQLiteDatabase = mySQLiteDatabase;
        this.cur = cur;
        this.type = type;
    }

    public SQLiteDatabase getMySQLiteDatabase() {
        return mySQLiteDatabase;
    }

    public void setMySQLiteDatabase(SQLiteDatabase mySQLiteDatabase) {
        this.mySQLiteDatabase = mySQLiteDatabase;
    }

    public Cursor getCur() {
        return cur;
    }

    public void setCur(Cursor cur) {
        this.cur = cur;
    }

    private void close(boolean closeDb) {
        try {
            if (cur != null) {
                cur.close();
            }
        }
        catch (Throwable e) {
            // TODO: handle Throwable
        }
        try {
            if (closeDb && mySQLiteDatabase != null) {
                if (type == 4) {
                    CarrierDBManager.close(mySQLiteDatabase);
                }
                else {
                    throw new Exception("unknown type:" + type);
                }
                mySQLiteDatabase = null;
            }
        }
        catch (Throwable e) {
            Log.e(Constant.TAG, "MixCursor close error:", e);
        }
    }

    public String[] getColumnNames() {
        if (cur != null)
            return cur.getColumnNames();

        return null;
    }

    public int getCount() {
        if (cur != null)
            return cur.getCount();

        return 0;
    }

    public double getDouble(int column) {
        if (cur != null)
            return cur.getDouble(column);

        return 0;
    }

    public float getFloat(int column) {
        if (cur != null)
            return cur.getFloat(column);

        return 0;
    }

    public int getInt(int column) {
        if (cur != null)
            return cur.getInt(column);

        return 0;
    }

    public long getLong(int column) {
        if (cur != null)
            return cur.getLong(column);

        return 0;
    }

    public short getShort(int column) {
        if (cur != null)
            return cur.getShort(column);

        return 0;
    }

    public String getString(int column) {
        if (cur != null)
            return cur.getString(column);
        return null;
    }

    public boolean isNull(int column) {
        if (cur != null)
            return cur.isNull(column);
        return false;
    }

    public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
        // TODO Auto-generated method stub
        if (cur != null)
            cur.copyStringToBuffer(columnIndex, buffer);
    }

    public void deactivate() {
        // TODO Auto-generated method stub
        if (cur != null)
            cur.deactivate();

    }

    public byte[] getBlob(int columnIndex) {
        if (cur != null)
            return cur.getBlob(columnIndex);
        return null;
    }

    public int getColumnCount() {
        // TODO Auto-generated method stub
        if (cur != null)
            return cur.getColumnCount();
        return 0;
    }

    public int getColumnIndex(String columnName) {
        if (cur != null)
            return cur.getColumnIndex(columnName);
        return -1;
    }

    public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException {
        if (cur != null)
            return cur.getColumnIndexOrThrow(columnName);
        return -1;
    }

    public String getColumnName(int columnIndex) {
        if (cur != null)
            return cur.getColumnName(columnIndex);
        return null;
    }

    public Bundle getExtras() {
        if (cur != null)
            return cur.getExtras();
        return null;
    }

    public int getPosition() {
        if (cur != null)
            return cur.getPosition();
        return -1;
    }

    public boolean getWantsAllOnMoveCalls() {
        if (cur != null)
            return cur.getWantsAllOnMoveCalls();
        return true;
    }

    public boolean isAfterLast() {
        if (cur != null)
            return cur.isAfterLast();
        return true;
    }

    public boolean isBeforeFirst() {
        // TODO Auto-generated method stub
        if (cur != null)
            return cur.isBeforeFirst();
        return true;
    }

    public boolean isClosed() {
        // TODO Auto-generated method stub
        if (cur != null)
            return cur.isClosed();
        return true;
    }

    public boolean isFirst() {
        if (cur != null)
            return cur.isFirst();
        return false;
    }

    public boolean isLast() {
        if (cur != null)
            return cur.isLast();
        return false;
    }

    public boolean move(int offset) {
        if (cur != null)
            return cur.move(offset);
        return false;
    }

    public boolean moveToFirst() {
        if (cur != null)
            return cur.moveToFirst();
        return false;
    }

    public boolean moveToLast() {
        if (cur != null)
            return cur.moveToLast();
        return false;
    }

    public boolean moveToNext() {
        if (cur != null)
            return cur.moveToNext();
        return false;
    }

    public boolean moveToPosition(int position) {
        // TODO Auto-generated method stub
        if (cur != null)
            return cur.moveToPosition(position);
        return false;
    }

    public boolean moveToPrevious() {
        // TODO Auto-generated method stub
        if (cur != null)
            return cur.moveToPrevious();
        return false;
    }

    public void registerContentObserver(ContentObserver observer) {
        // TODO Auto-generated method stub
        if (cur != null)
            cur.registerContentObserver(observer);

    }

    public void registerDataSetObserver(DataSetObserver observer) {
        if (cur != null)
            cur.registerDataSetObserver(observer);
    }

    public boolean requery() {
        if (cur != null)
            return cur.requery();
        return false;
    }

    public Bundle respond(Bundle extras) {
        if (cur != null)
            return cur.respond(extras);
        return null;
    }

    public void setNotificationUri(ContentResolver cr, Uri uri) {
        // TODO Auto-generated method stub
        if (cur != null)
            cur.setNotificationUri(cr, uri);
    }

    public void unregisterContentObserver(ContentObserver observer) {
        // TODO Auto-generated method stub
        if (cur != null)
            cur.unregisterContentObserver(observer);
    }

    public void unregisterDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub
        if (cur != null)
            cur.unregisterDataSetObserver(observer);
    }

    public static void closeCursor(MixCursor cur, boolean bl) {
        if (cur != null) {
            cur.close(bl);
        }
    }

}
